package handlers

import (
	"car-order-sys/cache"
	"car-order-sys/models"
	"encoding/json"
	"github.com/gin-gonic/gin"
	"strconv"
	"time"
)

func CarAdd(c *gin.Context) {
	car_name := c.PostForm("car_name")
	car_type := c.PostForm("car_type")
	car_price, _ := strconv.Atoi(c.PostForm("car_price"))
	if car_name == "" || car_type == "" || car_price == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	data := models.Car{
		CarName:  car_name,
		CarType:  car_type,
		CarPrice: car_price,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func CarOrderAdd(c *gin.Context) {
	order_code := c.PostForm("order_code")
	order_price, _ := strconv.Atoi(c.PostForm("order_price"))
	cid, _ := strconv.Atoi(c.PostForm("cid"))
	if order_code == "" || order_price == 0 || cid == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	data := models.Order{
		OrderCode:  order_code,
		OrderPrice: order_price,
		Cid:        cid,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "订单添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "订单添加成功",
	})
	return
}
func CarOrderList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "5"))
	offset := (page - 1) * size
	var carOrder []models.CarOrderList
	redis_key := "car_order_list_page_" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("orders").
			Select("orders.id as OrderId",
				"orders.order_code as OrderCode",
				"orders.order_price as OrderPrice",
				"cars.car_name as CarName",
				"cars.car_type as CarType",
				"cars.car_price as CarPrice",
			).
			Joins("left join cars on cars.id=orders.cid").
			Offset(offset).Limit(size).Find(&carOrder)
		jsonData, _ := json.Marshal(carOrder)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &carOrder)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": carOrder,
	})
}
func CarOrderDetail(c *gin.Context) {
	id := c.Query("id")
	var carOrder models.CarOrderList
	redis_key := "car_order_detail_" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("orders").
			Select("orders.id as OrderId",
				"orders.order_code as OrderCode",
				"orders.order_price as OrderPrice",
				"cars.car_name as CarName",
				"cars.car_type as CarType",
				"cars.car_price as CarPrice",
			).
			Joins("left join cars on cars.id=orders.cid").
			Where("orders.id = ?", id).Find(&carOrder)
		jsonData, _ := json.Marshal(carOrder)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &carOrder)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": carOrder,
	})
}
